package com.lantosec.hmrp.modules.eval.service.impl;

import com.lantosec.hmrp.core.util.DateUtils;
import com.lantosec.hmrp.modules.eval.entity.EvalApply;
import com.lantosec.hmrp.modules.eval.entity.EvalApplyDetail;
import com.lantosec.hmrp.modules.eval.entity.EvalType;
import com.lantosec.hmrp.modules.eval.mapper.EvalApplyMapper;
import com.lantosec.hmrp.modules.eval.service.IEvalApplyDetailService;
import com.lantosec.hmrp.modules.eval.service.IEvalApplyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lantosec.hmrp.modules.eval.vo.EvalApplyDetailVO;
import com.lantosec.hmrp.modules.eval.vo.EvalApplyVO;
import com.lantosec.hmrp.modules.system.entity.SysDept;
import com.lantosec.hmrp.modules.system.entity.SysUser;
import com.lantosec.hmrp.modules.system.service.ISysDeptService;
import com.lantosec.hmrp.modules.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.context.annotation.Primary;
import lombok.extern.log4j.Log4j2;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 奖惩申报 服务实现类
 * </p>
 *
 * @author AutoCode
 * @since 2020-06-23
 */
@Service
@Primary
@Log4j2
public class EvalApplyServiceImpl extends ServiceImpl<EvalApplyMapper, EvalApply> implements IEvalApplyService {
    @Autowired
    private IEvalApplyDetailService iEvalApplyDetailService;

    @Autowired
    private ISysDeptService iSysDeptService;

    @Autowired
    private ISysUserService iSysUserService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveEvalApply(EvalApplyVO vo) {
        if(Objects.nonNull(vo)){
            EvalApply evalApply = vo.convertToEntity();
            evalApply.setApplyTime(DateUtils.nowDate());
            this.save(evalApply);
            List<EvalApplyDetailVO> evalApplyDetails = vo.getEvalApplyList();
            evalApplyDetails.forEach(param->{
                EvalApplyDetail evalApplyDetail = param.convertToEntity();
                evalApplyDetail.setEvalApplyId(evalApply.getEvalApplyId());
                //查询项目的负责人
                String evalItemId = evalApplyDetail.getEvalItemId();
                EvalType evalType = baseMapper.getEvalItemCharge(evalItemId);
                evalApplyDetail.setChargeUserId(evalType.getManagerId());
                evalApplyDetail.setChargeUser(evalType.getManagerName());
                //判断奖惩形式是奖还是惩
                if(evalApplyDetail.getRewardType() == 2){
                    BigDecimal rewardValue = evalApplyDetail.getRewardValue();
                    if(null != rewardValue){
                        evalApplyDetail.setRewardValue(rewardValue.multiply(new BigDecimal(-1)));
                    }
                }
                //当责任对象是3-个人的情况，查询出所属科室
                if(evalApplyDetail.getTargetMode() == 3){
                    String[] targetUserId = evalApplyDetail.getTargetUserId().split("-");
                    SysDept sysDept = iSysDeptService.getById(targetUserId[0]);
                    //查询人员名称
                    SysUser sysUser = iSysUserService.getById(targetUserId[1]);
                    evalApplyDetail.setTargetUser(sysUser.getName());
                    evalApplyDetail.setTargetUserId(targetUserId[1]);
                    evalApplyDetail.setTargetDeptId(targetUserId[0]);
                    evalApplyDetail.setTargetDept(sysDept.getDeptName());
                }
                //当责任对象是1-科室的情况，需查出科室名称
                if(evalApplyDetail.getTargetMode() == 1){
                    String deptId = evalApplyDetail.getTargetDeptId();
                    String deptName = iSysDeptService.getById(deptId).getDeptName();
                    evalApplyDetail.setTargetDept(deptName);
                }
                iEvalApplyDetailService.save(evalApplyDetail);
            });
        }
    }
}
